<?php
namespace app\admin\controller;

use app\common\model\paths\PlatformModel;
use app\common\model\admin\AccountModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemUser;


/**
 * 商户账号管理
 * @package app\shop\controller
 * @author Lihongqi
 * @DateTime 2024-09-11 13:58:44
 */
class Account extends Controller
{

    /**
     * 账号管理
     * @auth true
     * @menu true
     * @author Lihongqi
     * @DateTime 2024-09-11 14:56:28
     * @return void
     */
    public function index ()
    {

        AccountModel::mQuery()->layTable(function () {
            $this->title   = lang('账号管理');
            $this->account_type  = AccountModel::getAccountType();
        }, function (QueryHelper $query) {

            $this->check_user = false;
            // 数据列表搜索过滤
            $query->like('title')->equal('status,edition')->dateBetween('create_at');

        });
    }

    protected function _index_page_filter (&$data)
    {

        $module_type   = AccountModel::moduleList();
        $account_type  = AccountModel::getAccountType();
        foreach ($data as &$item) {

            $module = array_map(function ($module_key) use ($module_type) {
                return $module_type[$module_key] ?? '';
            }, explode(',',$item['module']));

            $item['module'] = implode(',', $module);

            $item['edition'] = $account_type[$item['edition']?? ''] ?? '';

        }
    }

    /**
     * 添加
     * @auth true
     * @return void
     */
    public function add ()
    {

        AccountModel::mForm();
    }

    /**
     * 编辑
     * @auth true
     * @return void
     */
    public function edit ()
    {
        AccountModel::mForm();
    }


    protected function _form_filter (&$data)
    {
        if ($this->request->isGet()) {

            $this->module_type  = AccountModel::moduleList();

            $this->account_type = AccountModel::getAccountType();

            if ( !empty($data)) { // 如果是编辑
                $data['module'] = explode(',', $data['module']);//
            }
        }


        if ($this->request->isPost()) {

            $data['module'] = implode(',',$data['module']);

            empty($data['id']) && $data['create_at'] = date('Y-m-d H:i:s');
        }
    }


    /**
     * 删除
     *
     * @return void
     */
    public function remove ()
    {

        AccountModel::mDelete();
    }

    /**
     * 修改状态
     * @auth true
     */
    public function state()
    {

        AccountModel::mSave($this->_vali([
            'status.in:0,1'  => lang('状态值范围异常').'！',
            'status.require' => lang('状态值不能为空').'！',
        ]));
    }


}